/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmPrincipal.java
 *
 * Created on 17/03/2013, 14:11:17
 */
package ssga.gui;

import java.awt.Color;
import java.beans.PropertyVetoException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import ssga.dataIO.DataIO;

/**
 *
 * @author David
 */
public class frmPrincipal extends javax.swing.JFrame {

    public static List<JInternalFrame> listWin = new ArrayList<JInternalFrame>();

    //Lista de JInternalFrame a mostrar
    /** Creates new form frmPrincipal */
    public frmPrincipal() {
        initComponents();
        if (DataIO.init()) {
            this.lblStatus.setText("Conexion OK");
            this.lblStatus.setForeground(Color.GREEN);
        } else {
            this.lblStatus.setText("Conexion ERROR");
            this.lblStatus.setForeground(Color.RED);
        }
        this.setExtendedState(frmPrincipal.MAXIMIZED_BOTH);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelPrincipal = new javax.swing.JDesktopPane();
        jPanel1 = new javax.swing.JPanel();
        lblStatus = new javax.swing.JLabel();
        menuPrincipal = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        menuAlumno = new javax.swing.JMenu();
        menuAlumno1 = new javax.swing.JMenuItem();
        menuAlumno2 = new javax.swing.JMenuItem();
        menuDocente = new javax.swing.JMenu();
        menuDocente1 = new javax.swing.JMenuItem();
        menuVentanas = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        panelPrincipal.setName("panelPrincipal"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Estado"));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(983, Short.MAX_VALUE)
                .addComponent(lblStatus)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(lblStatus)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        menuPrincipal.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        jMenu1.setText("Archivo");
        menuPrincipal.add(jMenu1);

        menuAlumno.setText("Alumnos");

        menuAlumno1.setText("Agregar Alumno");
        menuAlumno1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuAlumno1ActionPerformed(evt);
            }
        });
        menuAlumno.add(menuAlumno1);

        menuAlumno2.setText("Buscar Alumo");
        menuAlumno2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuAlumno2ActionPerformed(evt);
            }
        });
        menuAlumno.add(menuAlumno2);

        menuPrincipal.add(menuAlumno);

        menuDocente.setText("Docentes");

        menuDocente1.setText("Agregar Docente");
        menuDocente1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuDocente1ActionPerformed(evt);
            }
        });
        menuDocente.add(menuDocente1);

        menuPrincipal.add(menuDocente);

        menuVentanas.setText("Ventanas");
        menuPrincipal.add(menuVentanas);

        setJMenuBar(menuPrincipal);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, 1025, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(10, 10, 10))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(panelPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, 516, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void menuAlumno1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuAlumno1ActionPerformed
// Accion al clickear en inscripcion alumnos

    if (!doFocus("Alta Alumnos")) {
        frmManageAlumnos fa = new frmManageAlumnos("new");
        fa.action = "Insert";
        fa.setTitle("Alta Alumnos");
        viewForm(fa);
//        this.panelPrincipal.add(fa);
//        fa.setVisible(true);
    }
}//GEN-LAST:event_menuAlumno1ActionPerformed
private void menuDocente1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuDocente1ActionPerformed
// Crear el objeto de la ventana y muestra el frame    
}//GEN-LAST:event_menuDocente1ActionPerformed

private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing

    if (this.panelPrincipal.getAllFrames().length > 0) {
        int opc = JOptionPane.showConfirmDialog(null,
                "¿Desea Cerrar? hay Ventanas Abiertas");
        if (JOptionPane.OK_OPTION == opc) {
            System.out.println("Cerrar");
            closeAllWin();
            // Cerrar la coneccion una vez que se cierra la ventana!
            DataIO.finish();
        } else {
            System.out.println("no borro nada...");            
        }
    }
}//GEN-LAST:event_formWindowClosing

    private void menuAlumno2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuAlumno2ActionPerformed

        if (!doFocus("Buscar Alumnos")) {
            frmListaAlumnos la = new frmListaAlumnos(this.panelPrincipal);
            la.setTitle("Buscar Alumnos");
            viewForm(la);
        }
    }//GEN-LAST:event_menuAlumno2ActionPerformed

    /**
     * Metodo que devueve si la ventana ta esta abierta o no 
     * @param title
     * @return Boolean
     */
    private boolean doFocus(String title) {
        boolean found = false;
        for (JInternalFrame f : frmPrincipal.listWin) {
            if (f.getTitle().equalsIgnoreCase(title)) {
                try {
                    f.setIcon(false);
                    found = true;
                    break;
                } catch (PropertyVetoException ex) {
                    System.out.println("Error en -->" + ex.toString());
                }
            }
        }
        return found;
    }

    //Prueba para mostrar panel
    public void viewForm(JInternalFrame frame) {
        this.panelPrincipal.add(frame);
        frame.show();
    }

    /**
     * Metodo que procesa el cerrar las ventanas
     */
    private void closeAllWin() {

        for (JInternalFrame jf : this.panelPrincipal.getAllFrames()) {
            jf.dispose();
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;




                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frmPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frmPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frmPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frmPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new frmPrincipal().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JMenu menuAlumno;
    private javax.swing.JMenuItem menuAlumno1;
    private javax.swing.JMenuItem menuAlumno2;
    private javax.swing.JMenu menuDocente;
    private javax.swing.JMenuItem menuDocente1;
    private javax.swing.JMenuBar menuPrincipal;
    private javax.swing.JMenu menuVentanas;
    private javax.swing.JDesktopPane panelPrincipal;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the panelPrincipal
     */
    public javax.swing.JDesktopPane getPanelPrincipal() {
        return panelPrincipal;
    }

    /**
     * @param panelPrincipal the panelPrincipal to set
     */
    public void setPanelPrincipal(javax.swing.JDesktopPane panelPrincipal) {
        this.panelPrincipal = panelPrincipal;
    }
}
